<?php

	header('Access-Control-Allow-Origin: *');
	header("Access-Control-Allow-Method:POST,GET");  
	header ( "Content-Type: text/html; charset=UTF-8" );
	require_once './mysql.class.php';
	require_once './response.class.php';

	//获取数据
	$rawPostData = file_get_contents("php://input");
	$postData = json_decode($rawPostData,true);
	$content = $postData['content'];
	$sex = $postData['sex'];

	//获取ip
	function getIp(){
		if (getenv("HTTP_CLIENT_IP"))
			$ip = getenv("HTTP_CLIENT_IP"); 
		else if(getenv("HTTP_X_FORWARDED_FOR"))
			$ip = getenv("HTTP_X_FORWARDED_FOR"); 
		else if(getenv("REMOTE_ADDR")) 
			$ip = getenv("REMOTE_ADDR"); 
		else
			$ip = time().rand(0,100);
		return $ip;
	}
	$ip = getIp();
	

    //设置缓存
	$memcache = new Memcache();
	$memcache->connect('127.0.0.1', 11111) or die ("Could not connect");
	if( $memcache->get('confession_Publish_'.$ip) ){
		$message = array(
		    'content'=>'两次发表时间是1个小时哦~',
		);
		$re = new Response();
		$re->show(200,'fail',$message);
	}else{
		$memcache->set('confession_Publish_'.$ip,'TRUE',0,3600);
	}

	//判断数据
	if(empty($content)){
		$message = array(
			'content'=>'提交的内容不能为空~',		
		);
		$re = new Response();
		$re->show(200,'fail',$message);
	}
	if(mb_strlen($content,"utf-8")>300){
		$message = array(
		    'content'=>'提交的文字超过范围~',        
		);
		$re = new Response();
		$re->show(200,'fail',$message);
	}

	//插入数据库
	$sqlhelper = new sqlhelper();
	$array = array('content'=>$content,'ip'=>$ip,'sex'=>$sex);
	$table = "blxbconfession_content";
	$affectedRows = $sqlhelper->insert($table,$array);
	$sqlhelper->close_connect();

	//echo $affectedRows;
	//返回信息
	$re = new Response();
	if($affectedRows > 0){
		$message = array(
			'content'=>'提交数据成功',
		);
		$re->show(200,'success',$message);
	}else{
		$message = array(
			'content'=>'提交数据失败,请@留言,联系公众号~',
		);
		$re->show(200,'fail',$message);
	}
	exit();
	


